package com.user.controller;

import com.common.domain.pojo.Result;
import com.user.authentication.LoginAuthentication;
import com.user.authentication.LoginDetail;
import com.user.domain.dto.CheckLoginDto;
import com.user.domain.dto.ForceLoginDto;
import com.user.domain.dto.RegisterDto;
import com.user.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class LoginController {

    final IUserService iUserService;

    @PostMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response,@RequestBody LoginDetail loginDetail){
        iUserService.login(request,response,loginDetail);
    }

    @PostMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response){
        iUserService.logout(request,response);
    }

    // 用户注册
    @PostMapping("/register")
    public Result<String> register(@RequestBody RegisterDto registerDto){
        iUserService.registerUser(registerDto);
        return Result.success();
    }

    // 获取登入验证码
    @GetMapping("/sms/code")
    public Result<String> getSmsCode(String phone){
        iUserService.sendSmsPhoneCodeByLogin(phone);
        return Result.success();
    }

    // 校验账号是否已经登入
    @GetMapping("/login/check")
    public Result<Boolean> checkLogin(@RequestParam(value = "username",required = false) String username, @RequestParam(value = "phone", required = false) String phone){
        boolean checkLogin = iUserService.checkLogin(username,phone);
        return Result.success(checkLogin);
    }
}
